﻿$.MGJ.ProductList = [];
(function () {
    var _this = $.MGJ.ProductList;
    $.extend($.MGJ.ProductList, {
        conditionModel: {
            pageIndex: 1,
            pageSize: 10,
            searchKey: null,//查询条件
            showSys: 0,  //我的模板：0  系统模板：1 我的模板和系统模板：2
            classifyId: 0 // 节点ID  等于0查询全部
        },
        // 新增产品列表
        Dialog_AddListObj: null,
        Dialog_AddList: function () {
            _this.Dialog_AddListObj = $.L.dialog({
                url: ACTION_URL("ProductAdd_V", "Product"),
                title: "新增产品列表",
                width: 1100,
                height: 'auto',
                buttons: {
                    //"button_LastStep": {
                    //    type: 'primary hidden',
                    //    text: '上一步',
                    //    click: function () {
                    //        $("#productMenu").click();
                    //    }
                    //},
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function (e) {
                            //if ($("button[data-btnname='button_save']").text() == "下一步") {

                            //    if ($("#CategoryId").val() != "") {
                            //        $("#ForPower").click()
                            //    } else {
                            //        $.L.msgError("未选产品类型")

                            //        return false;
                            //    }

                            //}//保存按钮
                            //else {
                            //list - con
                            if ($("input[name='ImageUrl1']").val() != "") {
                                var bootstrapValidator = $("#product_form").data('bootstrapValidator');
                                bootstrapValidator.validate();
                                if (bootstrapValidator.isValid()) {
                                    _this.Ajax_saveProduct();
                                    $(".btn-primary").attr("disabled", true);
                                }
                                else {
                                    $.L.msgError("请检查数据格式！");
                                };
                            } else {
                                $.L.msgError("产品图片(整体图)未上传！");
                            }



                            //}

                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        },
        //编辑产品列表
        Dialog_EditListObj: null,
        Dialog_EditList: function (obj) {
            var pid = $(obj).attr("productId");
            _this.Dialog_EditListObj = $.L.dialog({
                url: ACTION_URL("ProductEdit_V", "Product") + "?pid=" + pid,
                title: "编辑产品列表",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            if ($("input[name='ImageUrl1']").val() != "") {
                                var bootstrapValidator = $("#product_form").data('bootstrapValidator');
                                bootstrapValidator.validate();
                                if (bootstrapValidator.isValid()) {
                                    _this.Ajax_saveProduct();
                                    $(".btn-primary").attr("disabled", true);
                                }
                                else {
                                    $.L.msgError("请检查数据格式！");
                                };
                            }
                            else {
                                $.L.msgError("产品图片(整体图)未上传！");
                            }
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        },
        //修改  增加 产品  保存
        Ajax_saveProduct: function () {
            if ($("#Subheading").val() == "") {
                $("#Subheading").val(" ");
            }
            if ($("#TaoBaoUrl").val() == "") {
                $("#TaoBaoUrl").val(" ");
            }
            if ($("#WXUrl").val() == "") {
                $("#WXUrl").val(" ");
            }
            if ($("#Constitution").val() == "") {
                $("#Constitution").val(" ");
            }
            var chk_value = "";
            $('input[name="ApproveChe"]:checked').each(function () {
                chk_value += $(this).val() + ",";
            });

            var CustomInfoJson = _this.GetCustomInfo();

            $("#CustomInfo").val(CustomInfoJson);

            $("#Approve").val(chk_value.substring(0, chk_value.length - 1));
            $.L.ajaxHandle({
                url: ACTION_URL("saveProduct", "Product") + "?TransitionPath=" + $("#TransitionPath").val(),
                data: $("#product_form").serialize(),
                success: function (result) {
                    if (result > 0) {
                        if (_this.Dialog_EditListObj != undefined) {
                            _this.Dialog_EditListObj.close();
                        } else {
                            _this.Dialog_AddListObj.close();
                        }
                        $.L.msgSuccess("操作成功");
                        _this.Ajax_GetProductList(_this.conditionModel.pageIndex);
                    }
                }
            });
        },
        Ajax_GetProductList: function (pageIndex, pageSize) {
            var _index = 1;
            window.SearchKey = removeSpace($("#EnterpriseName").val());
            _index = pageIndex;         
            pageSize=10;
            $.L.ajaxHandle({
                url: ACTION_URL("GetProductList", "Product") + "?pageindex=" + pageIndex+"&pageSize="+pageSize,
                data: $("#SearchForm").serialize(),
                success: function (response) {
                    _this.ProductList = response;
                    $("#ProductData").L_bindingDataList(response);
                }
            });
        },
        //删除
        Ajax_DelProduct: function (item) {
            var _id = $(item).attr("productId");
            if (!_id || _id === '') {
                $.L.msgWarning("当前无法执行该操作", function () {
                    _this.Ajax_GetProductList(_this.conditionModel.pageIndex);
                });
            }
            $.L.msgConfirm("确认删除该产品?", function () {
                $.L.ajaxHandle({
                    url: ACTION_URL("DeleteProduct", "Product") + "?id=" + _id,
                    success: function (respone) {

                        _this.Ajax_GetProductList(_this.conditionModel.pageIndex);
                        $.L.msgSuccess("删除完成");
                    }
                })
            })
        },
        CutButtonInfo: function () {
            //if ($("#CategoryId").val() != "") {
            //    $("button[data-btnname='button_LastStep']").attr("class", "btn btn-primary");
            //    $("button[data-btnname='button_save']").text("保存");
            //} else {
            //    $.L.msgError("未选产品类型")
            //}
        },
        CutButtonMenu: function () {
            $("button[data-btnname='button_save']").text("下一步");
            $("button[data-btnname='button_LastStep']").attr("class", "btn btn-primary hidden");
        },
        //Ajax_GetTableTree: function (id) {
        //    $.L.ajaxHandle({
        //        url: ACTION_URL("getClassifyTree", "Product") + "?EID=" + id,
        //        success: function (data) {

        //            gridTree = new TableTree4J("gridTree", "../");
        //            gridTree.tableDesc = "<table border=\"1\" class=\"GridView table table-bordered \" width=\"100%\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse: collapse\"  bordercolordark=\"#C0C0C0\" bordercolorlight=\"#C0C0C0\" bordercolorlight=\"#C0C0C0\" >";
        //            var headerDataList = new Array("菜单名");
        //            var widthList = new Array("20%", "30%", "10%", "30%");
        //            gridTree.setHeader(headerDataList, -1, widthList, true, "tittle-bg");
        //            //设置列样式
        //            gridTree.gridHeaderColStyleArray = new Array("", "", "", "centerClo1");
        //            gridTree.gridDataCloStyleArray = new Array("", "", "", "centerClo2");
        //            $("#sel_parent_meun").html("<option value='0'>无</option>");
        //            $(data).each(function (i, d) {
        //                var MenuName = "";
        //                if (d.PID != 0) {
        //                    //MenuName += "<span onclick='javascript:$(\"#CategoryId\").val(" + d.ID + ")'>" + d.Name + "</span>";
        //                    MenuName += "<span menuname=\"" + d.ID + "\" >" + d.Name + "</span>";
        //                }
        //                else {
        //                    MenuName += "<span>" + d.Name + "</span>";
        //                }

        //                var dataList = new Array(MenuName);

        //                //1：对象，2：编号，3：父节点 默认为0，null，5:顺序
        //                gridTree.addGirdNode(dataList, d.ID, d.PID == 0 ? -1 : d.PID, null, d.Sequence);
        //                //$("#sel_parent_meun").append("<option value='" + d.ID + "'>" + d.MenuName + "</option>");
        //            });
        //            gridTree.printTableTreeToElement("gridTreeDiv");
        //        }
        //    })
        //},
        Ajax_srcHandle: function (val) {
            if (val == "" || val == undefined) {
                return "/Content/userImgs/Product/ProductNullImg.png";
            } else {
                return val;
            }

        },

        //CustomModel: {
        //    key: "",
        //    value: ""
        //},



        GetCustomInfo: function () {
            var domList = $("#CustomInfoWrap .row");

            var CustomModel = function () {
                this.key="",
                this.value=""
            }


            var array = [];

            $.each(domList, function (item) {

                var exModel = domList[item];
                var innerDivList = exModel.children[0].children;

                var input1 = innerDivList[0].children[0];
                var input2 = innerDivList[2].children[0];
                customModel = new CustomModel();
                customModel.key = input1.value;
                customModel.value = input2.value;
                array[item] = customModel;
            });


            var json = JSON.stringify(array);

            return json

        },


        AppendCustomInfo:function(customJson){
            var array = JSON.parse(customJson);
            for (var i = 0; i < array.length; i++) {

                _this.AddCustomInfo(array[i]);

            }
        },


        AddCustomInfo: function (customModel) {
            if (customModel == undefined) {
                var CustomModel = function () {
                    this.key = "",
                    this.value = ""
                }
                customModel = new CustomModel();
            }
            var html = [
                '<div class="row">',
                        '<div class="container">',
                            '<div class="col-xs-offset-2 col-xs-3 col-sm-3 col-md-3 col-lg-3">',
                                '<input class="form-control" type="text" value="' + customModel.key + '"/>',
                            '</div>',
                            '<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 text-center">：</div>',
                            '<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3">',
                                '<input class="form-control" type="text" value="' + customModel.value + '" />',
                            '</div>',
                            '<div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"  style="line-height: 2.3em;">',
                                '<div class="glyphicon glyphicon-remove" onclick="$.MGJ.ProductList.RemoveCustomInfo(this)"></div>',
                            '</div>',
                        '</div>',
                    '</div>'
            ].join("")
            $("#CustomInfoWrap").append(html);

            //_this.GetCustomInfo();
        },
        RemoveCustomInfo: function (_this) {
            var x=$(_this).parents(".row").remove()
        },
        ExpirationDateType: function (num) {
            ExpirationDateType += num
            ExpirationDateType < 0 ? ExpirationDateType = 0 : ExpirationDateType;
            ExpirationDateType > 2 ? ExpirationDateType = 2 : ExpirationDateType;
            switch (ExpirationDateType)
            {
                case 0:
                    $("#ExpirationDateType").text("天")
                    $("input[name='ExpirationDateType']").val("天")
                    break;
                case 1:
                    $("#ExpirationDateType").text("月")
                    $("input[name='ExpirationDateType']").val("月")
                    break;
                default:
                    $("#ExpirationDateType").text("年")
                    $("input[name='ExpirationDateType']").val("年")
            }
        }
    })
})(jQuery);









